<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Auth extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $list= \app\admin\model\Auth::select();
        $list = getTree($list);
        return  view('auth/index',['list'=>$list]);
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create(Request $request)
    {
        if (\request()->isPost()){
            //接受数据
            $data=  $request->param();
            //入库
            \app\admin\model\Auth::create($data,true);
           return ['msg'=>200];


        }else{
            $top_nav=\app\admin\model\Auth::where(['pid' => 0])->select();

            return view('auth/create',['top_nav' => $top_nav]);
        }


        //
    }





    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        if (!\request()->isPost()){
            $data = \app\admin\model\Auth::where('id',$id)->find();
            //所有顶级权限
            $top_nav = \app\admin\model\Auth::where('pid',0)->select();
            return view('auth/edit',['data' =>$data,'top_nav' => $top_nav]);
        }else{
            //接受数据
            $data = $request->param();
            //入库
            \app\admin\model\Auth::update($data,true);
            return ['msg'=>200];
        }



    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
        public function delete($id)
    {

        $auth = \app\admin\model\Auth::destroy($id);
        if ($auth){
            return  ['msg' => 201];
        }
        return ['msg' => 202];
        //
    }
}
